%
% Calculates the centroid of an interval-represented fuzzy set x 
% It assumes that the alpha values are evenly distriubuted amongst the intervals,
% however normality is not required.
%
function c = fuzzy_centroid2(x)

N = length(x);

am_index = 2 .* [1:N-1] - 1;

% weughted modified index
wm_index = 3 .* [1:N-1].' - 2;

% weighted integral of the left function
wl_term = (x(2:end,1) - x(1:end-1,1)).' * (x(2:end,1) + (x(2:end,1) + x(1:end-1,1)) .* wm_index);

% weighted integral of the right function
wr_term = (x(1:end-1,2) - x(2:end,2)).' * (x(2:end,2) + (x(2:end,2) + x(1:end-1,2)) .* wm_index);

% weighted area of the peak
wp_term = 3 .* (N - 1) .* (x(end,2) - x(end,1)) .* (x(end,2) + x(end,1));

% Total weighted areal
wa = (wl_term + wr_term) + wp_term;

% area
A = 3 .* (am_index * (x(2:end,1) - x(1:end-1,1) + x(1:end-1,2) - x(2:end,2))) + 6 .* (N -1) .* (x(end,2) - x(end,1));

% normalized by nonweighted area for the centroid
c = wa / A;
